package com.supinfo.suptracking.servlet;

import com.supinfo.suptracking.entity.Zone;
import com.supinfo.suptracking.service.ZoneService;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Fabien, Jérémy, Ludovic, Brice
 */

@WebServlet(name = "RemoveZoneServlet", urlPatterns = {"/auth/removeZone"})
public class RemoveZoneServlet extends HttpServlet {
    
    @EJB
    private ZoneService zoneServ;
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request,response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Get form fields
        String zoneIdTemp = request.getParameter("zoneIdRemove");
        
        // Transform id in long
        Long zoneId=Long.valueOf(zoneIdTemp);
         
        Zone zonedel = zoneServ.processGetZoneById(zoneId);
                 
        // Remove zone
        zoneServ.processDelZone(zonedel);
         
        // Event + redirect
        request.getSession().setAttribute("event", "1");
        request.getSession().setAttribute("message", "Zone deleted");
        response.sendRedirect(getServletContext().getContextPath()+"/auth/zones");
    }
}
